package com.allen.controller;

import com.allen.bean.Category;
import com.allen.bean.Setmeal;
import com.allen.bean.SetmealDish;
import com.allen.common.R;
import com.allen.dto.SetmealDto;
import com.allen.service.CategoryService;
import com.allen.service.SetmealDishService;
import com.allen.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dragon1006xu@gmail.com
 * @create 2022/2/19 10:17
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;
    @Autowired
    CategoryService categoryService;

    //套餐管理分页查询
    @GetMapping("/page")
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        //设置分页条件【当前页码和每页显示条数】,使用page对象设置
        Page<Setmeal> setmealPage = new Page<>(page, pageSize);
        //返回的数据封装到Page<SetmealDto>中
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //设置分页查询条件
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据套餐名称进行模糊查询
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name), Setmeal::getName, name);
        //执行查询条件 查询之后会将total、records重新加入到Page对象中
        setmealService.page(setmealPage, lambdaQueryWrapper);
        //将查询出来的套餐分页信息数据setmealPage封装到setmealDtoPage对象中，setmealDtoPage=setmealPage+categoryName
        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> setmealList = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : setmealList) {
            //创建setmealDto对象，将每个setmeal对象填充到setmealDto对象中
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //根据当前套餐中的category_id查询到category_name将套餐所属分类名称封装到setmealDto对象中
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            //将每个封装好的setmealDto对象存入到List<SetmealDto>集合中
            setmealDtoList.add(setmealDto);
        }
        setmealDtoPage.setRecords(setmealDtoList);
        //返回分页数据 Page 封装到R对象中
        return R.success(setmealDtoPage);
    }

    //新增套餐信息
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("套餐信息:{}", setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }

    //删除套餐
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        log.info("要删除的id:{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功！");
    }

    /**
     * 根据条件查询套餐数据
     *
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,
                Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,
                Setmeal::getStatus, setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);

    }

    /**
     * 更新套餐状态
     *
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public R update(@RequestParam List<Long> ids, @PathVariable Integer status) {
        //根据status修改停售或启售状态
        setmealService.updateStatus(ids, status);
        return R.success("更新套餐状态成功");
    }

    /**
     * 回显套餐管理修改信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getByIdWithDish(@PathVariable Long id) {
        //根据套餐id查询到的套餐信息以及对应的菜品信息，封装到SetmealDto并返回
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功！");
    }

    @GetMapping("/dish/{id}")
    public R<Setmeal> getSetmeal(@PathVariable Long id) {
        return R.success(setmealService.getById(id));
    }
}
